package main

import (
	"net"
	"fmt"
	"os"
	"time"
	"strconv"
	"git.oschina.net/solate/test/other/tcp/protocol"
)

func main() {
	server := "127.0.0.1:9998"
	tcpAddr, err := net.ResolveTCPAddr("tcp", server)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fatal err:%s", err.Error())
		os.Exit(1)
	}

	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fatal err:%s", err.Error())
		os.Exit(1)
	}


	fmt.Println("connect success")
	serder(conn)
	
}

func serder(conn net.Conn)  {
	defer conn.Close()
	for i := 0; i < 100; i++ {
		session:=GetSession()
		words := "{\"ID\":"+ strconv.Itoa(i) +"\",\"Session\":"+session +"2015073109532345\",\"Meta\":\"golang\",\"Content\":\"message\"}"
		conn.Write(protocol.Enpack([]byte(words)))
	}
	fmt.Println("send over")

}


func GetSession() string{
	gs1:=time.Now().Unix()
	gs2:=strconv.FormatInt(gs1,10)
	return gs2
}